<template>
  <!-- 如果内容比较多可以添加top属性 -->
  <el-dialog
    top="10"
    :title="title"
    :visible.sync="dialogVisible"
    width="30%"
    :before-close="handleClose">
    <el-form ref="form" status-icon :model="form" :rules="rules" label-width="80px">
      <el-form-item label="角色名称" prop="name" style="width:90%;">
        <el-input :disabled="disabled" @keyup.enter.native="save()" class="inputText" v-model="form.name" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="权限分配" prop="roleMenus">
        <el-tree
          ref="tree"
          default-expand-all
          :data="menuList"
          show-checkbox
          :default-checked-keys="defaultCheckList"
          @check="selectMenu"
          node-key="id">
        </el-tree>
      </el-form-item>
    </el-form>
    <div class="bottomDiv">
      <el-button @click="handleClose">关闭</el-button>
      <el-button :disabled="disabled" type="primary" @click="save">保存</el-button>
    </div>
  </el-dialog>
</template>

<script>
import api from '@/api'
export default {
  props: {
    dialogVisible: {
      type: Boolean,
      default: false
    },
    disabled: {
      type: Boolean,
      default: false
    }
  },
  name: 'RoleEdit',
  data () {
    return {
      defaultCheckList: [21],
      selectMenuList: [],
      menuList: [],
      title: '',
      form: {
        name: '',
        id: null,
        roleMenus: []
      },
      rules: {
        name: [
          { required: true, message: '请输入角色名称', trigger: 'blur' }
        ],
        roleMenus: [
          { required: true, message: '请选择权限', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    selectMenu (data, data1) {
      this.selectMenuList = data1.checkedNodes
      var par = []
      this.selectMenuList.forEach(res => {
        var parVo = {
          menuId: res.id
        }
        if (res.layer !== 1) {
          par.push(parVo)
        }
      })
      this.form.roleMenus = par
    },
    setTitle (title) {
      this.title = title
    },
    initData () {
      if (this.$refs.form !== undefined) {
        this.$refs.form.resetFields()
      }
      this.form.name = null
      this.form.id = null
      this.form.roleMenus = []
      this.initMenusData()
    },
    setData (data) {
      if (this.$refs.form !== undefined) {
        this.$refs.form.resetFields()
      }
      this.form.name = data.name
      this.form.id = data.id
      this.form.roleMenus = data.roleMenus
      this.initMenusData()
    },
    async updateApi () {
      await api.UPDATE_ROLE(this.form)
      this.$emit('initTableData')
      this.$message.success('操作成功')
      this.handleClose()
    },
    async saveApi () {
      await api.SAVE_ROLE(this.form)
      this.$emit('initTableData')
      this.$message.success('操作成功')
      this.handleClose()
    },
    save () {
      this.$refs.form.validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            this.$confirm('此操作将永久修改该数据, 是否继续?', '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning'
            }).then(() => {
              this.updateApi()
            }).catch(() => {
              this.$message({
                type: 'info',
                message: '已取消修改'
              })
            })
          } else {
            this.saveApi()
          }
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    handleClose (done) {
      this.$emit('close')
    },
    recursionMenus (menus) {
      var childrens = []
      for (var i = 0; i < menus.children.length; i++) {
        var menu = menus.children[i]
        var children = this.recursionMenus(menu)
        var menuJson = {
          id: menu.menuId,
          label: menu.title,
          type: menu.type,
          layer: menu.layer
        }
        if (children.length > 0) {
          menuJson.children = children
        }
        if (this.disabled) {
          menuJson.disabled = this.disabled
        }
        childrens.push(menuJson)
      }
      return childrens
    },
    async initMenusData () {
      var par = {}
      const res = await api.GET_MENU_LIST(par)
      var menus = []
      for (var i = 0; i < res.body.length; i++) {
        var menu = res.body[i]
        var children = this.recursionMenus(menu)
        var menuJson = {
          id: menu.menuId,
          label: menu.title,
          type: menu.type,
          layer: menu.layer
        }
        if (children.length > 0) {
          menuJson.children = children
        }
        if (this.disabled) {
          menuJson.disabled = this.disabled
        }
        menus.push(menuJson)
      }
      this.menuList = menus
      var arr = []
      for (var k = 0; k < this.form.roleMenus.length; k++) {
        var menuSelect = this.form.roleMenus[k]
        arr.push(menuSelect.menuId)
      }
      this.defaultCheckList = arr
    }
  }
}
</script>

<style>
  .inputText {
    width: 70%;
  }
  .bottomDiv {
    width: 100%;
    text-align: center;
  }
</style>
